﻿Ext.define('app.prm.detail.CMOProgress', {
    extend: 'Ext.panel.Panel',
    border: true,
    title: '项目进度表',
    projectId: 0,
    layout: 'fit',
    initComponent: function () {
        var me = this; 
        me.items = this.getGrid();
        this.getGrid().store.reload();
        me.callParent();
    },
    getGrid: function () {
        if (!this.grid) {
            var main = this;
            this.grid = Ext.create('Ext.grid.Panel', {

                plugins: [
                    Ext.create('Ext.grid.plugin.CellEditing', {
                        clicksToEdit: 1,
                        listeners: {
                            beforeedit: 'onEditPhase'
                        }
                    })
                ],
                tbar: [{ text: '刷新', handler: function () { main.grid.store.reload();}}],
                columns: [
                    {
                        text: "#",
                        dataIndex: "dataType",
                        locked: true,
                        align: "center"
                    },
                {
                    text: "物料名称<br/>或代号",
                    dataIndex: "name",
                    locked: true,
                    align: "center"
                },
                {
                    text: "到货信息，通（优化)路线<br/>合成，项管",
                    align: "center",
                    columns: [
                                {
                                    text: "预计时间",
                                    dataIndex: "throughSynthP",
                                    format: 'Y-m-d',
                                    editor:
                                        {
                                            xtype: 'datefield',
                                            format: 'Y-m-d'
                                        },
                                    align: "center"
                                },
                                {
                                    text: "实际时间",
                                    dataIndex: "throughSynthA",
                                    align: "center"
                                },
                                {
                                    text: "备注",
                                    dataIndex: "age",
                                    align: "center"
                                }
                    ]
                },
                {
                    text: "EHS数据（包括MSDS）/评估<br/>安全，合成",
                    align: "center",
                    columns: [
                                {
                                    text: "预计时间",
                                    dataIndex: "name",
                                    align: "center"
                                },
                                {
                                    text: "实际时间",
                                    dataIndex: "sex",
                                    align: "center"
                                },
                                {
                                    text: "备注",
                                    dataIndex: "age",
                                    align: "center"
                                }
                    ]
                },
                {
                    text: "分析方法建立（签发为准）<br/>ARD",
                    align: "center",
                    columns: [
                                {
                                    text: "预计时间",
                                    dataIndex: "name",
                                    align: "center"
                                },
                                {
                                    text: "实际时间",
                                    dataIndex: "sex",
                                    align: "center"
                                },
                                {
                                    text: "备注",
                                    dataIndex: "age",
                                    align: "center"
                                }
                    ]
                },
                {
                    text: "质量标准订立（签发为准）<br/>合成，ARD,CRD",
                    align: "center",
                    columns: [
                                {
                                    text: "预计时间",
                                    dataIndex: "name",
                                    align: "center"
                                },
                                {
                                    text: "实际时间",
                                    dataIndex: "sex",
                                    align: "center"
                                },
                                {
                                    text: "备注",
                                    dataIndex: "age",
                                    align: "center"
                                }
                    ]
                },
                {
                    text: "车间SOP起草<br/>合成，车间，CRD",
                    align: "center",
                    columns: [
                                {
                                    text: "预计时间",
                                    dataIndex: "name",
                                    align: "center"
                                },
                                {
                                    text: "实际时间",
                                    dataIndex: "sex",
                                    align: "center"
                                },
                                {
                                    text: "备注",
                                    dataIndex: "age",
                                    align: "center"
                                }
                    ]
                }
                ],
                store: Ext.create("app.ux.PrmStore",
                    {
                        url: '/prm/home/GetCmoProgressData',
                        autoLoad: false,
                            listeners: {
                                load: function () {
                                   // mergeCells(main.grid.getId(), [1]);
                                }
                            }

                })
                //    Ext.create('Ext.data.Store', {

                //    fields: ['dataType', 'name',
                //        'throughSynthP', 'throughSynthA', 'throughSynthR',
                //        'EHSDataP', 'EHSDataA', 'EHSDataR',
                //        'AnalysisMethodP', 'AnalysisMethodA', 'AnalysisMethodR',
                //        'QualityStandardP', 'QualityStandardA', 'QualityStandardR',
                //        'SOPP', 'SOPA', 'SOPR'
                //    ],
                //    listeners: {
                //        load: function () {
                //            mergeCells(main.grid, [1]);
                //        }
                //    }
                //})
            });
        }
        return this.grid;
    }
})

/** 
* 合并单元格 
* @param {} grid  要合并单元格的grid对象 
* @param {} cols  要合并哪几列 [1,2,4] 
*/

function mergeCells(gridId, cols) {
    var arrayTr = document.getElementById(gridId + "-body").firstChild.firstChild.firstChild
            .getElementsByTagName('tr');
    var trCount = arrayTr.length;
    var arrayTd;
    var td;
    var merge = function (rowspanObj, removeObjs) { //定义合并函数  
        if (rowspanObj.rowspan != 1) {
            arrayTd = arrayTr[rowspanObj.tr].getElementsByTagName("td"); //合并行  
            td = arrayTd[rowspanObj.td - 1];
            td.rowSpan = rowspanObj.rowspan;
            td.vAlign = "middle";
            Ext.each(removeObjs, function (obj) { //隐身被合并的单元格  
                arrayTd = arrayTr[obj.tr].getElementsByTagName("td");
                arrayTd[obj.td - 1].style.display = 'none';
            });
        }
    };
    var rowspanObj = {}; //要进行跨列操作的td对象{tr:1,td:2,rowspan:5}  
    var removeObjs = []; //要进行删除的td对象[{tr:2,td:2},{tr:3,td:2}]  
    var col;

    Ext.each(cols, function (colIndex) { //逐列去操作tr  
        var rowspan = 1;
        var divHtml = null; //单元格内的数值  
        for (var i = 1; i < trCount; i++) { //i=0表示表头等没用的行  
            arrayTd = arrayTr[i].getElementsByTagName("td");
            var cold = 0;
            Ext.each(arrayTd, function (Td) { //获取RowNumber列和check列  
                if (Td.getAttribute("class")
                        .indexOf("x-grid-cell-special") != -1)
                    cold++;
            });
            col = colIndex + cold; //跳过RowNumber列和check列  
            if (!divHtml) {
                divHtml = arrayTd[col - 1].innerHTML;
                rowspanObj = {
                    tr: i,
                    td: col,
                    rowspan: rowspan
                }
            } else {
                var cellText = arrayTd[col - 1].innerHTML;
                var addf = function () {
                    rowspanObj["rowspan"] = rowspanObj["rowspan"] + 1;
                    removeObjs.push({
                        tr: i,
                        td: col
                    });
                    if (i == trCount - 1)
                        merge(rowspanObj, removeObjs);//执行合并函数  
                };
                var mergef = function () {
                    merge(rowspanObj, removeObjs);//执行合并函数  
                    divHtml = cellText;
                    rowspanObj = {
                        tr: i,
                        td: col,
                        rowspan: rowspan
                    }
                    removeObjs = [];
                };
                if (cellText == divHtml) {
                    if (colIndex != 1) {
                        var leftDisplay = arrayTd[col - 2].style.display;//判断左边单元格值是否已display  
                        if (leftDisplay == 'none') {
                            addf();
                        } else {
                            mergef();
                        }
                    } else {
                        addf();
                    }
                } else {
                    mergef();
                }
            }
        }
    });
};